package com.jun.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;

@Component
public class LogHandlerInterceptor implements HandlerInterceptor {
  private static final Logger log = LoggerFactory.getLogger(LogHandlerInterceptor.class);

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    log.info("请求方式: " + request.getMethod());
    log.info("请求路径: " + request.getRequestURL());

    StringJoiner sj = new StringJoiner(",", "请求参数: {", "}");
    Map<String, String[]> parameterMap = request.getParameterMap();
    parameterMap.forEach((k, v) -> sj.add(k + ": " + Arrays.toString(v)));

    log.info(sj.toString());
    return true;
  }
}
